package com.xizi.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.content.Media;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.net.URI;
import java.util.List;

@RestController
@RequestMapping("/demo")
public class DemoController {

    private final ChatClient chatClient;

    public DemoController(ChatClient.Builder builder) {
        this.chatClient = builder
                .defaultSystem("你是小舒的专属答疑AI") // 会发现这个模型有点笨，有时会反应不出来我给他新定义的身份
                .defaultAdvisors(new SimpleLoggerAdvisor())
                .build();
    }

    @RequestMapping("/send")
    public String send(String message) {
        return chatClient
                .prompt(message)
                .call()
                .content();
    }

    @RequestMapping(value = "/send2",produces = "text/html;charset=utf-8")
    public Flux<String> send2(String message) {
        return chatClient
                .prompt(message)
                .stream()
                .content();
    }

    record Actor(String name, List<String> movingName){}

    @RequestMapping(value = "send3",produces = "text/html;charset=utf-8")
    public Flux<String> send3(String name) {
        return chatClient
                .prompt(String.format("告诉我这个 %s 的著名电影有哪些",name))
                .stream()
                .content();
    }

    @RequestMapping(value = "send4",produces = "text/html;charset=utf-8")
    public String send4(String name) {
        return chatClient
                .prompt(String.format("告诉我这个 %s 的著名电影有哪些",name))
                .call()
                .entity(Actor.class)
                .toString();
    }

    //设置默认的System Message

    @Autowired
    private ChatClient chatClient2;// config 中 配置

    @RequestMapping(value = "send5",produces = "text/html;charset=utf-8")
    public Flux<String> send5(String message) {
        return chatClient2.prompt(message)
                .system(sp -> sp.param("word","亲"))
                .stream()
                .content();
    }

    // 下面的代码就不行
    @RequestMapping(value = "send6",produces = "text/html;charset=utf-8")
    public Flux<String> send6(String message) {
        return chatClient.prompt(message)
                .system(sp -> sp.param("word","亲"))
                .stream()
                .content();
    }


    // 上面的代码用<version>1.0.0-M6.1</version>-->  AI 依赖




    /**
     *      多模态
     *
     */
    @GetMapping("/image")
    public String image(String prompt) throws Exception {
        String url= "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg";
        List<Media> mediaList = List.of(new Media(MimeTypeUtils.IMAGE_PNG, new URI(url).toURL().toURI()));
        UserMessage message = UserMessage.builder().text(prompt).media(mediaList).build();
        ChatResponse response = chatClient2
                .prompt(new Prompt(message))
                .call()
                .chatResponse();
        return response.getResult().getOutput().getText();
    }



}
